﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Device
{
    public partial  class Audiences
    {
        public enum AuthResult
        {
            Success, PasswordDoesnotMatch, Lock, DoesnotMatch, ExistEmail, Add, InExistid, Del, Existid,InExistDevid,Ownerid
        }

        //判断邮箱和密码是否匹配
        public static AuthResult auth(string email, string password)
        {
            using (Model1Container db = new Model1Container())
            {
                Audience sh = new Audience();
                List<Audience> sheb = (from u in db.Audience集 where u.email == email && u.password == password select u).ToList();
                if (sheb.Count() > 0)
                {
                    List<Audience> qu = (from u in db.Audience集 where u.email == email && u.loginpower   == "禁用" select u).ToList();
                    if (qu.Count() > 0)
                    {
                        return AuthResult.Lock;
                    }
                    else
                    {
                        return AuthResult.Success;
                    }
                }
                else
                {
                    return AuthResult.PasswordDoesnotMatch;
                }
            }
        }

        //找回密码时校验用户、问题、答案
        public static AuthResult queryUserQueAnser(string email, string question, string answer)
        {
            using (Model1Container db = new Model1Container())
            {
                Audience cm = new Audience();
                List<Audience> sheb = (from u in db.Audience集 where u.email == email && u.question == question && u.answer == answer select u).ToList();
                if (sheb.Count() > 0)
                {
                    return AuthResult.Success;
                }
                else
                {
                    return AuthResult.DoesnotMatch;
                }
            }
        }

        //判断邮箱是否注册
        public static AuthResult Email (string email)
        {           
            using (Model1Container db = new Model1Container())
            {              
                    List<Audience> sheb = (from u in db.Audience集 where u.email == email select u).ToList();
                    if (sheb.Count() > 0)
                    {
                        return AuthResult.ExistEmail;
                    }
                    else
                    {
                        return AuthResult.Add;
                    }                
            }
        }

        //判断用户ID是否存在
        public static AuthResult UserId(Int32 userid,Int32 ownerid)
        {
            using (Model1Container db = new Model1Container())
            {
                Audience sh = new Audience();
                List<Audience> sheb = (from u in db.Audience集 where u.id == userid && u.ownerid == ownerid select u).ToList();
                if (sheb.Count() > 0)
                {
                    return AuthResult.Del ;
                }
                else
                {
                    return AuthResult.InExistid  ;
                }
            }
        }


        //判断设备ID是否存在
        public static AuthResult DeviceId(Int32 deviceid, Int32 ownerid)
        {          
            using (Model1Container db = new Model1Container())
            {
                Equiment ch = new Equiment();
                Accesstable sh = new Accesstable();
                List<Equiment> id = (from u in db.Equiment集 where u.id == deviceid && u.ownerid == ownerid select u).ToList();
                if (id.Count() > 0)
                {
                    List<Accesstable> sheb = (from u in db.Accesstable集 where u.equimentid == deviceid select u).ToList();
                    if (sheb.Count() > 0)
                    {
                        return AuthResult.Existid;
                    }
                    else
                    {
                        return AuthResult.InExistDevid;
                    }
                }
                else
                {
                    return AuthResult.Ownerid;
                }
            }
        }
    }
}